package com.yxjx.duoxue.payment;

import java.util.ArrayList;

import com.yxjx.duoxue.R;
import com.yxjx.duoxue.Yx;
import com.yxjx.duoxue.customview.YxGridView;
import com.yxjx.duoxue.customview.YxListView;
import com.yxjx.duoxue.datamodel.DMProduct;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CourseProductsView extends LinearLayout {
	
	private static final int DEFAULT_UNEXPANDED_NUM = 3;
	
	private View mRoot;
	private YxListView mListView;
	private ArrayList<DMProduct> mDMProducts;
	private ProductsAdapter mAdapter;
	
	// 课程详情页为true， 商品选择列表为false
	private boolean isCollpased = false;
	
	public CourseProductsView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		mRoot = LayoutInflater.from(context).inflate(R.layout.segment_course_products_function, this, true);
		mListView = (YxListView) findViewById(R.id.list);
		mAdapter = new ProductsAdapter();
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// 打开相应的商品页面
					
				Intent intent = new Intent(getContext(),
						ProductInfoActivity.class);
				intent.putExtra(Yx.KEY_PRODUCT_OBJECT, mDMProducts.get(arg2));
				getContext().startActivity(intent);
			}
		});
	}
	
	/**
	 * set Data and update UI
	 * @param products
	 */
	public void setData(ArrayList<DMProduct> products, boolean isCollpased) {
		mDMProducts = products;
		this.isCollpased = isCollpased;
		updateViewMore();
		if (mDMProducts == null || mDMProducts.size() == 0) {
			this.setVisibility(View.GONE);
			invalidate();
		} else {
			mAdapter.notifyDataSetChanged();
		}
	}
	
	private int getProductCount() {
		return mDMProducts == null ? 0 : mDMProducts.size();
	}
	
	private void updateViewMore() {
		// 商品选择列表页面
		if (!isCollpased) {
			mRoot.findViewById(R.id.head).setVisibility(View.GONE);
		}
		
		if (isCollpased && getProductCount() > DEFAULT_UNEXPANDED_NUM) {
			mRoot.findViewById(R.id.view_more_products).setVisibility(View.VISIBLE);
			mRoot.findViewById(R.id.view_more_products_above_divider).setVisibility(View.VISIBLE);
			UIHelper.setText(mRoot.findViewById(R.id.view_more_products),
					R.id.products_num, "查看更多优惠（" + getProductCount() + "）");
		} else {
			mRoot.findViewById(R.id.view_more_products).setVisibility(View.GONE);
			mRoot.findViewById(R.id.view_more_products_above_divider).setVisibility(View.GONE);
		}
	}

	class ProductsAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			int actualCount = mDMProducts == null ? 0 : mDMProducts.size();
			if (isCollpased) {
				return actualCount <= DEFAULT_UNEXPANDED_NUM ? actualCount : DEFAULT_UNEXPANDED_NUM;
			} else {
				return actualCount;
			}
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View ret = null;
			if (arg1 == null) {
				ret = LayoutInflater.from(getContext()).inflate(
						R.layout.item_course_product, null);
			} else {
				ret = arg1;
			}
		    Utils.setPriceText((TextView) ret.findViewById(R.id.product_price), mDMProducts.get(arg0));
			UIHelper.setText(ret, R.id.product_title, mDMProducts.get(arg0).getProductName());
			return ret;
		}
		
	}
}
